package com.itheima.server.imp;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.NursingPlanMapper;
import com.itheima.mapper.NursingProjectPlanMapper;
import com.itheima.pojo.*;
import com.itheima.server.INursingPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 护理计划Service业务层处理
 * 
 * @author alexis
 * @date 2025-06-02
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
@Autowired
private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public PageResult<NursingPlan> getList(NursingPage nursingPage) {
        PageHelper.startPage(nursingPage.getPage(),nursingPage.getPageSize());
        List<NursingPlan> List = nursingPlanMapper.getlist(nursingPage);
        Page<NursingPlan> p = (Page<NursingPlan>) List;
        return new PageResult<>(p.getTotal(), List);
    }
@Transactional
    @Override
    public Object insertNursingPlan(NursingPlanDto dto) {
    NursingPlan nursingPlan = new NursingPlan();
    BeanUtils.copyProperties(dto, nursingPlan);
    nursingPlan.setCreateTime(LocalDateTime.now());
    nursingPlanMapper.insert(nursingPlan);

    // 2.只有当有项目计划时才批量保存关联关系
    if (dto.getProjectPlans() != null && !dto.getProjectPlans().isEmpty()) {
        nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
    }

    return 1; // 插入成功返回1
    }


}
